package com.trust.pams.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.atp.AnalysisToolPak;
import org.apache.poi.ss.formula.eval.FunctionEval;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class DataUtil {
    private static Logger logger = Logger.getLogger(DataUtil.class);
    /**
     * 判断对象是否Empty(null或元素为0)<br>
     * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
     * 
     * @param pObj
     *            待检查对象
     * @return boolean 返回的布尔值
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object pObj) {
        if (pObj == null)
            return true;
        if ("".equals(pObj))
            return true;
        if (pObj instanceof String) {
            if (((String) pObj).length() == 0) {
                return true;
            }
        } else if (pObj instanceof Collection) {
            if (((Collection) pObj).size() == 0) {
                return true;
            }
        } else if (pObj instanceof Map) {
            if (((Map) pObj).size() == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断对象是否为NotEmpty(!null或元素>0)<br>
     * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
     * 
     * @param pObj
     *            待检查对象
     * @return boolean 返回的布尔值
     */
    @SuppressWarnings("rawtypes")
    public static boolean isNotEmpty(Object pObj) {
        if (pObj == null)
            return false;
        if ("".equals(pObj))
            return false;
        if (pObj instanceof String) {
            if (((String) pObj).length() == 0) {
                return false;
            }
        } else if (pObj instanceof Collection) {
            if (((Collection) pObj).size() == 0) {
                return false;
            }
        } else if (pObj instanceof Map) {
            if (((Map) pObj).size() == 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 数据格式化.
     * 
     * @param pattern
     *            the pattern
     * @param i
     *            the i
     * @return the string
     */
    public static String codeFormat(String pattern, Object value) {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(value);
    }

    /**
     * 格式化时间.
     * 
     * @param date
     *            the date
     * @return the string
     */
    public static String fomatDate(String date) {
        if (isNotEmpty(date)) {
            return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
        }
        return null;
    }

    /**
     * 将double数值格式化成指定格式的字串
     * 
     * @param value
     * @param format
     * @return
     */
    public static Double formatCurrency2Double(Double value, String format) {
        if (DataUtil.isEmpty(format)) {
            format = "0.00";
        }
        DecimalFormat df = new DecimalFormat(format);
        String temp = df.format(value);
        return Double.valueOf(temp);
    }

    /**
     * 格式化时间.
     * 
     * @param date
     *            the date
     * @return the string
     */
    public static String fomatLongDate(String date) {
        if (isNotEmpty(date)) {
            return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + " "
                    + date.substring(8, 10) + ":" + date.substring(10, 12) + ":" + date.substring(12, 14);
        }
        return null;
    }

    /**
     * 格式化时间.
     * 
     * @param date
     *            the date
     * @return the string
     */
    public static String fomatDateTime2String(String date) {
        if (isNotEmpty(date)) {
            return date.replace("-", "").replace("T", "").replace(":", "").replace(" ", "");
        }
        return null;
    }

    /**
     * 将时间字符串格式化成一个日期(java.util.Date)
     * 
     * @param dateStr
     *            要格式化的日期字符串，如"2014-06-15 12:30:12"
     * @param formatStr
     *            格式化模板，如"yyyy-MM-dd HH:mm:ss"
     * @return the string
     */
    public static Date formatDateString2Date(String dateStr, String formatStr) {
        DateFormat dateFormat = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = dateFormat.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将时间字符串格式化成一个日期(java.util.Date)
     * 
     * @param dateStr
     *            要格式化的日期字符串，如"2014-06-15 12:30:12"
     * @param formatStr
     *            格式化模板，如"yyyy-MM-dd HH:mm:ss"
     * @return the string
     */
    public static String formatDate2String(Date date, String formatStr) {
        DateFormat dateFormat = new SimpleDateFormat(formatStr);
        String result = null;
        try {
            result = dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 将一个毫秒数时间转换为相应的时间格式
     * 
     * @param longSecond
     * @return
     */
    public static String formateDateLongToString(long longSecond) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(longSecond);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(gc.getTime());
    }

    public static String formateDateLongToString(long longSecond, String formatPatten) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(longSecond);
        if (DataUtil.isEmpty(formatPatten)) {
            formatPatten = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat format = new SimpleDateFormat(formatPatten);
        return format.format(gc.getTime());
    }
    
    /**
     * 往前多少分钟
     * getTimeByMinute: <br/>
     *
     * @author Gordon HR
     * @param minute
     * @return
     * @since JDK 1.6
     */
    public static String getTimeByMinute(int minute) {

        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.MINUTE, minute);

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

    }
    
    /**
     * 格式化金额.
     * 
     * @param value
     *            the value
     * @return the string
     */
    public static String formatCurrency2String(Long value) {
        if (value == null || "0".equals(String.valueOf(value))) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(value / 100.00);
    }

    /**
     * 格式化金额.
     * 
     * @param value
     *            the value
     * @return the string
     */
    public static String formatCurrency2String(BigDecimal value) {
        if (value == null) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(value);
    }

    /**
     * 格式化金额.
     * 
     * @param priceFormat
     *            the price format
     * @return the long
     */
    public static Long formatCurrency2Long(String priceFormat) {
        BigDecimal bg = new BigDecimal(priceFormat);
        Long price = bg.multiply(new BigDecimal(100)).longValue();
        return price;
    }

    /**
     * 获取当前时间.
     * 
     * @param currentDate
     *            the current date @return @throws
     */
    public static String getToDayStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    /**
     * 获取当前时间当作文件名称
     * 
     * @return
     */
    public static String getToDayStrAsFileName() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    /**
     * 获取当前时间：yyyyMMddHHmmss
     * 
     * @return
     */
    public static String getToDayStr1() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }
    
    public static String getToDayStr2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }
    
    /**
     * 
     * getYesterdayStr2: <br/>
     *
     * @author 
     * @param currentDate
     * @param dfff
     * @return
     * @throws ParseException
     * @since JDK 1.6
     */
    public static String getYesterdayStr2(String currentDate,int dfff) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(currentDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -dfff);
        String nextDate = sdf.format(calendar.getTime());
        return nextDate;
    }

    public static Date getToDay() throws ParseException {
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // Date date = sdf.parse(String.valueOf(System.currentTimeMillis()));
        return new Date();
    }

    /**
     * 获取下一天.
     * 
     * @param currentDate
     *            the current date
     * @return the next date str
     * @throws ParseException
     *             the parse exception
     */
    public static String getNextDateStr(String currentDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = sdf.parse(currentDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        String nextDate = sdf.format(calendar.getTime());
        return nextDate;
    }

    /**
     * 获取上一天.
     * 
     * @param currentDate
     *            the current date
     * @return the next date str
     * @throws ParseException
     *             the parse exception
     */
    public static String getYesterdayStr(String currentDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = sdf.parse(currentDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        String nextDate = sdf.format(calendar.getTime());
        return nextDate;
    }

    /**
     * 
     * getDiffDays:获取前diff天 <br/>
     *
     * @author pan
     * @param currentDate
     * @return
     * @throws ParseException
     * @since JDK 1.6
     */
    public static Date getDiffDays(String currentDate, int diff) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date;
        try {
            date = sdf.parse(currentDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, 0 - diff);
            return calendar.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }

    /**
     * 获取上一周.
     * 
     * @param currentDate
     *            the current date
     * @return the next date str
     * @throws ParseException
     *             the parse exception
     */
    public static String getLastWeekStr(Date currentDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //Date date = sdf.parse(currentDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DATE, -6);
        String nextDate = sdf.format(calendar.getTime());
        return nextDate;
    }

    /**
     * getlastMonth:得到当前日期的前一月的日期
     *
     * @author wm
     * @param currentDate
     * @return
     * @throws ParseException
     * @since JDK 1.6
     */
    public static String getlastMonth(Date currentDate, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.MONTH, -1);
        String nextDate = sdf.format(calendar.getTime());
        return nextDate;
    }

    /**
     * 根据日期获取星期
     * 
     * @param strdate
     * @return
     */
    public static String getWeekDayByDate(String strdate) {
        final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        try {
            date = sdfInput.parse(strdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayOfWeek < 0)
            dayOfWeek = 0;
        return dayNames[dayOfWeek];
    }

    /**
     * 生成固定长度的随机字符和数字
     * 
     * @param len
     * @return
     */
    public static String generateRandomCharAndNumber(Integer len) {
        StringBuffer sb = new StringBuffer();
        for (Integer i = 0; i < len; i++) {
            int intRand = (int) (Math.random() * 52);
            int numValue = (int) (Math.random() * 10);
            char base = (intRand < 26) ? 'A' : 'a';
            char c = (char) (base + intRand % 26);
            if (numValue % 2 == 0) {
                sb.append(c);
            } else {
                sb.append(numValue);
            }
        }
        return sb.toString();
    }

    public static String readPropertiesFile(String key) {
        String fileName = "/filepath_win.properties";
        String filePath = "";
        InputStream inputStream = DataUtil.class.getResourceAsStream(fileName);
        Properties pros = new Properties();
        try {
            pros.load(inputStream);
            filePath = pros.getProperty(key);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return filePath;
    }

    /**
     * 方法描述：将系统限定的路径转换为绝对正确的路径
     * 
     * @param originalPath
     * @return
     */
    public static String getGeneralFilePath(String originalPath) {
        if ((null != originalPath) && !("".equals(originalPath))) {
            String strPath[] = originalPath.split("\\\\|/");
            originalPath = "";
            // 拼接jar路径
            for (int i = 0; i < strPath.length; i++) {
                if (!("".equals(strPath[i])) && !("".equals(strPath[i].trim()))) {
                    originalPath = originalPath + strPath[i].trim();
                    if (i < strPath.length - 1) {
                        originalPath = originalPath + File.separator;
                    }
                }
            }
        }
        return originalPath;
    }

    // /**
    // * 往文件写数据
    // */
    // public static void WriteFile(String content, String filePath,
    // String fileName) throws IOException {
    //
    // File file = new File(filePath);
    // if (!file.exists()) {
    // file.mkdirs();
    // }
    // File f1 = new File(filePath + File.separator + fileName);
    // FileUtils.writeStringToFile(f1, content, "utf-8");
    //// try {
    ////
    //// FileOutputStream fos = new FileOutputStream(f1);
    //// fos.write(content.getBytes());
    //// fos.close();
    ////
    //// } catch (IOException e) {
    //// System.out.println("Uh oh, got an IOException error!");
    //// e.printStackTrace();
    //// }
    // }

    /**
     * 复制文件(以超快的速度复制文件)
     * 
     * @param srcFile
     *            源文件File
     * @param destDir
     *            目标目录File
     * @param newFileName
     *            新文件名
     * @return 实际复制的字节数，如果文件、目录不存在、文件为null或者发生IO异常，返回-1
     */
    public static long copyFile(File srcFile, File destFile) {
        long copySizes = 0;
        FileChannel fcin = null;
        FileChannel fcout = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcFile);
            fcin = fis.getChannel();
            fos = new FileOutputStream(destFile);
            fcout = fos.getChannel();
            long size = fcin.size();
            fcin.transferTo(0, fcin.size(), fcout);
            copySizes = size;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fcin) {
                try {
                    fcin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fcout) {
                try {
                    fcout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return copySizes;
    }

    /**
     * 删除单个文件
     * 
     * @param fileName
     *            被删除文件的文件名
     * @return 单个文件删除成功返回true,否则返回false
     */
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.isFile() && file.exists()) {
            file.delete();
            System.out.println("删除单个文件" + fileName + "成功！");
            return true;
        } else {
            System.out.println("删除单个文件" + fileName + "失败！");
            return false;
        }
    }

    public static void makeDir(File dir) {
        if (!dir.getParentFile().exists()) {
            makeDir(dir.getParentFile());
        }
        dir.mkdir();
    }

    /**
     * decimalFormat: 千分位展示数字<br/>
     *
     * @author guooo
     * @param obj
     * @return
     * @since JDK 1.6
     */
    public static String decimalFormat(long obj) {
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(obj);
    }

    /**
     * 
     * decimalFormatDouble: <br/>
     *
     * @author zq
     * @return
     * @since JDK 1.6
     */
    public static String decimalFormatDouble(Double obj) {
        if (obj == 0 || obj == 0.0) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("#,###.00");
        return df.format(obj);
    }
    
    public static String decimalFormatDouble1(Double obj) {
    	if (obj == 0 || obj == 0.0) {
    		return "0.00";
    	}
    	DecimalFormat df = new DecimalFormat("##.##");
    	return df.format(obj);
    }

    /**
     * formatDouble2: <br/>
     * double保留小数位
     * 
     * @author wm
     * @param obj
     * @param rate
     * @return
     * @since JDK 1.6
     */
    public static double formatDouble2(double obj, int rate) {
        BigDecimal bg = new BigDecimal(obj).setScale(rate, RoundingMode.HALF_UP);
        return bg.doubleValue();
    }

  
    /**
     * formatBankNo: <br/>
     * 格式化银行卡：每四位加空格
     * 
     * @author wm
     * @param bankNo
     * @return
     * @since JDK 1.6
     */
    public static String formatBankNo(String bankNo) {
        String rt = "";
        if (isNotEmpty(bankNo)) {
            String bankNoNew = bankNo.trim().replaceAll(" ", "");
            int l = bankNoNew.length();
            StringBuilder sb = new StringBuilder(bankNoNew);
            for (int i = 0; i < l; i++) {
                if (i % 5 == 0) {
                    sb.insert(i, " ");
                }
            }
            rt = sb.toString();
        }
        return rt;
    }

    //获取输入月份的第一天
    public static Date getMonthFirst(String month) {

        return null;
    }

    //获取输入月份的最后一天
    public static Date getMonthLast(String month) {

        return null;
    }

    /**
     * 根据身份证计算年龄 IdNOToAge: <br/>
     *
     * @author Yan
     * @param IdNO
     * @return
     * @since JDK 1.6
     */
    public static String IdNOToAge(String IdNO) {
        if (DataUtil.isEmpty(IdNO)) {
            return "";
        }
        int leh = IdNO.length();
        String dates = "";
        if (leh == 18) {
            dates = IdNO.substring(6, 10);
            SimpleDateFormat df = new SimpleDateFormat("yyyy");
            String year = df.format(new Date());
            int u = Integer.parseInt(year) - Integer.parseInt(dates);
            return u + "";
        } else {
            dates = IdNO.substring(6, 8);
            return dates;
        }
    }
    
    /**
     * 根据身份证计算年龄 IdNOToSex: <br/>
     *
     * @author Yan
     * @param IdNO
     * @return
     * @since JDK 1.6
     */
    public static String IdNOToSex(String IdNO) {
        if (DataUtil.isEmpty(IdNO)) {
            return "";
        }
        int leh = IdNO.length();
        String dates = "";
        String sGender = "未知";
        if (leh == 18) {
            dates = IdNO.substring(16, 17);
            if (Integer.parseInt(dates) % 2 != 0) {
                sGender = "男";//男
            } else {
                sGender = "女";//女
            }
            return sGender;
        } else {
            dates = IdNO.substring(14, 15);
            if (Integer.parseInt(dates) % 2 != 0) {
                sGender = "男";//男
            } else {
                sGender = "女";//女
            }
            return sGender;
        }
    }

    public static void main(String[] args) throws Exception {
//        pdf2Image("人保投保单","http://10.1.9.150/M00/00/35/CgEJllwZ_JaACyw4AAPbwILHrp4683.pdf", "C:\\Users\\wangmeng\\Desktop\\pdf2img","C:\\Users\\wangmeng\\Desktop\\pdfTemp", 100);
        System.out.println(getBirAgeSex("432503198705220332"));
      String day=  getToDayStr2();
      System.out.println(formatDateString2Date("20190529104731","yyyyMMddHHmmss"));
    }

    /**
     * 根据浏览器类型获取 设置 -对应编码的导出文件名 getNameByAgent: <br/>
     *
     * @author Yan
     * @param request
     * @param filename
     * @return
     * @throws UnsupportedEncodingException
     * @since JDK 1.6
     */
    public static String getNameByAgent(HttpServletRequest request, String filename) throws Exception {
        String agent = request.getHeader("USER-AGENT").toLowerCase();
        //根据浏览器类型处理文件名称
        if ((agent.indexOf("msie") > -1) || (agent.indexOf("edge") > -1) || (agent.indexOf("trident") > -1)) {
            filename = URLEncoder.encode(filename, "UTF-8");
        } else { //firefox/safari不转码
            filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
        }
        return filename;
    }

    /**
     * refreshFormula:刷新带公式的cell的值（先写值再调用此方法） <br/>
     *
     * @author wangmeng
     * @param wb
     * @param sheets
     * @since JDK 1.6
     */
    public static void refreshFormula(Workbook wb,List<String> sheets){
        for(String s:sheets){

        	Sheet sheet = wb.getSheet(s);
            if(DataUtil.isNotEmpty(sheet.getSheetName())){
            	//注册自定义excel公式。。。
                updateFormula(wb, sheet);
                sheet.setForceFormulaRecalculation(true);
            }
        }
    }
    
    public static  void updateFormula(Workbook wb,Sheet sheet){
        int firstRow=sheet.getFirstRowNum();
        int lastRow=  sheet.getLastRowNum();
        FormulaEvaluator eval=null;
        if(wb instanceof HSSFWorkbook)
            eval=new HSSFFormulaEvaluator((HSSFWorkbook) wb);
        else if(wb instanceof XSSFWorkbook)
            eval=new XSSFFormulaEvaluator((XSSFWorkbook) wb);
        for(int i=firstRow;i<=lastRow;i++){
            Row r=sheet.getRow(i);
            if(null!=r){
                Cell c=null;
                for(int m=r.getFirstCellNum();m<r.getLastCellNum();m++){
                    c=r.getCell(m);
                    if(null!=c&&c.getCellType()==Cell.CELL_TYPE_FORMULA)
                        eval.evaluateFormulaCell(c);
                    wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(c);
                }  
            }
        }
    }
    
    
 
    private static boolean createDirectory(String folder) {
        File dir = new File(folder);
        if (dir.exists()) {
            return true;
        } else {
            return dir.mkdirs();
        }
    }
    
    /**
     * 通过身份证号码获取出生日期、性别、年龄
     * @param certificateNo
     * @return 返回的出生日期格式：1990-01-01   性别格式：F-2，M-1
     */
    public static Map<String, String> getBirAgeSex(String certificateNo) {
        String birthday = "";
        String age = "";
        String sexCode = "";

        int year = Calendar.getInstance().get(Calendar.YEAR);
        char[] number = certificateNo.toCharArray();
        boolean flag = true;
        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag) return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag) return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        }
        if (flag && certificateNo.length() == 15) {
            birthday = "19" + certificateNo.substring(6, 8) + "-"
                    + certificateNo.substring(8, 10) + "-"
                    + certificateNo.substring(10, 12);
            sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length())) % 2 == 0 ? "2" : "1";
            age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
        } else if (flag && certificateNo.length() == 18) {
            birthday = certificateNo.substring(6, 10) + "-"
                    + certificateNo.substring(10, 12) + "-"
                    + certificateNo.substring(12, 14);
            sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1)) % 2 == 0 ? "2" : "1";
            age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("birthday", birthday);
        map.put("age", age);
        map.put("sexCode", sexCode);
        return map;
    }



}